package com.ruoyi.web.controller.example;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Anonymous
@RestController
@Api(tags = "异常处理相关内容")
public class ExceptionController {

    @GetMapping("/divTwoNumber")
    @ApiOperation(value = "两数相除，测试异常")
    //写一个方法，构造一个异常，主动捕获，并且返回
    public AjaxResult divTwoNumber(int num1,int num2){
        //计算两数相除
        int result;
        try{
            result = num1 / num2;
        }catch (ArithmeticException e){
            throw new BizException(500,"num2不能为0");
        }catch (Exception e){
            throw new BizException(500,"其他异常");
        }
        //这里是正常情况
        return AjaxResult.success("num1 除以 num2 的值是：" + result);
    }
}
